package com.example.demo.common;

//定义返回结果

//使用泛型：因为Object可以接收任何类型
//在返回结果时就可以用泛型去约束一下
public class AppResult<T> {

    //自定义状态码
    private int code;

    //描述信息
    private String message;

    private T data;//查询数据

    //构造方法
    //有的返回里没有查询
    public AppResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //成功方法
    //有data加泛型，没有data不加泛型
    /**
     * 成功
     * @param message 描述信息
     * @param data  具体的数据
     * @return
     * @param <T>
     */
    public static <T> AppResult<T> success(String message,T data){
        return new AppResult<>(ResultCode.SUCCESS.code, message,data);
    }

    public static AppResult success(String message){
        return new AppResult<>(ResultCode.SUCCESS.code, message,null);
    }

    public static <T> AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS.code, ResultCode.SUCCESS.getMessage(),data);
    }

    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),null);
    }

    //失败

    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(ResultCode resultCode){
        return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
